package com.guigu.serviceorder.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.common_util.pojo.utils.Result;

import com.guigu.serviceorder.entity.*;
import com.guigu.serviceorder.mapper.OrderAndOrderItemMapper;
import com.guigu.serviceorder.mapper.TbOrderItemMapper;
import com.guigu.serviceorder.mapper.TbOrderLogMapper;
import com.guigu.serviceorder.mapper.TbOrderMapper;
import com.guigu.serviceorder.opfeign.OpFeignSkuSpu;
import com.guigu.serviceorder.pojo.OrderAndOrderItem;
import com.guigu.serviceorder.service.OrderAndOrderItemService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

@Service
public class OrderAndOrderItemImpl implements OrderAndOrderItemService {

    @Autowired
    private OrderAndOrderItemMapper orderAndOrderItemMapper;

    @Autowired
    TbOrderMapper orderMapper;

    @Autowired
    TbOrderItemMapper orderItemMapper;

    @Autowired
    TbOrderLogMapper orderLogMapper;

    @Autowired
    OpFeignSkuSpu opFeignSkuSpu;

    @Override
    public Result queryById(int id) {

        OrderAndOrderItem orders = new OrderAndOrderItem();//创建组合实体
        TbOrder order = orderMapper.selectById(id);//得到主订单
        if(null==order){
            return null;
        }
        // 订单id
        String orderId = order.getId();

        // 获取订单详情的数据
        QueryWrapper wrapper =new QueryWrapper();
        wrapper.eq("order_id",orderId);
        List<TbOrderItemUtil> orderItemUtilList = new ArrayList<>();
        List<TbOrderItem> list1 = orderItemMapper.selectList(wrapper);

//        for (TbOrderItem list2: list1
//        ) {
//
//            tbOrderItemUtil.setTbOrderItem(list2);
//            tbOrderItemUtil.setProductPic("");
//            tbOrderItemUtil.setProductAttr("");
//            orderItemUtilList.add(tbOrderItemUtil);
//
//        }
        Iterator<TbOrderItem> iterator = list1.iterator();
        while (true) {
            if (iterator.hasNext()) {
                TbOrderItemUtil tbOrderItemUtil = new TbOrderItemUtil();
                TbOrderItem next = iterator.next();
                tbOrderItemUtil.setTbOrderItem(next);
                tbOrderItemUtil.setProductPic("");
                tbOrderItemUtil.setProductAttr("");
                orderItemUtilList.add(tbOrderItemUtil);
            }else {
                break;
            }

        }


        // 根据id 进行订单消息的获取
        QueryWrapper wrapper1 = new QueryWrapper();
        wrapper1.eq("order_id",orderId);
        List<TbOrderLog> orderLogList = orderLogMapper.selectList(wrapper1);

        // 获取查询sku的string类型 id数组
        List<String> listString = new ArrayList<>();

        System.out.println(orderItemUtilList.get(0));

        for (TbOrderItemUtil orderItemUtil: orderItemUtilList
             ) {
            listString.add(orderItemUtil.getTbOrderItem().getSkuId());
        }

        // 获取远程调用的feign sku数据
        Map<String, Object> searchMap = new HashMap();
        String join = StringUtils.join(listString, ',');
        searchMap.put("skuId",join);
        // 获取sku对象
        Result list = this.opFeignSkuSpu.findList(searchMap);
        List<Object> list2 =(List<Object>) list.getData().get("list");

        // 循环拿出想要的数据
        List<TbOrderItemUtil> orderItemListNew = new ArrayList<>();

        // LinkedHashMap 转化
        List<TbSku> handlerModelList = new ArrayList<>();
        for(Object obj:list2) {
            handlerModelList.add(JSON.parseObject(JSONObject.toJSONString(obj, true),TbSku.class));
        }

        for (TbSku sku : handlerModelList) {
            for (TbOrderItemUtil item : orderItemUtilList) {
                String skuId = sku.getId();
                String itemSkuId = item.getTbOrderItem().getSkuId();
                if (skuId.equals(itemSkuId)) {
                    item.setProductPic(sku.getImage());
                    item.setProductAttr(sku.getName());

                    orderItemListNew.add(item);
                }
            }
        }

        System.out.println(handlerModelList);

        OrderAndOrderItem orderAndOrderItem = new OrderAndOrderItem();
        orderAndOrderItem.setOrder(order);
        orderAndOrderItem.setOrderItem(orderItemListNew);
        orderAndOrderItem.setOrderLogs(orderLogList);

        return Result.ok().data("orderAndOrderItem",orderAndOrderItem);
    }
}
